package cn.softeem.tushu.web;



import cn.softeem.tushu.po.*;
import cn.softeem.tushu.service.Impl.MainServiceImpl;
import cn.softeem.tushu.service.MainService;
import cn.softeem.tushu.utils.BaseServlet;
import cn.softeem.tushu.utils.PageBean;
import cn.softeem.tushu.utils.Tools;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "MainServlet", urlPatterns = "/MainServlet")
public class MainServlet extends BaseServlet {

    private MainService mainService=new MainServiceImpl();
    public String left(HttpServletRequest request, HttpServletResponse response)  {
        List<Category> categoryTwo = mainService.getCategoryTwo();
        request.setAttribute("list2",categoryTwo);
//        request.getRequestDispatcher("/jsps/left.jsp").forward(request,response);
        return "f:/jsps/left.jsp";
    }

    public String list(HttpServletRequest request, HttpServletResponse response)  {
        String x = request.getParameter("x");
        String pageNow =request.getParameter("pageNow");
        String type = request.getParameter("type");
        if (pageNow==null || pageNow ==""){
            pageNow="1";
        }
        PageBean<Book> book = mainService.getBookList(x, Integer.parseInt(pageNow),type);
        book.setUrl("MainServlet?method=list&type="+type+"&x="+x);
        request.setAttribute("bean",book);
//        request.getRequestDispatcher("/jsps/book/list.jsp").forward(request,response);
        return "f:/jsps/book/list.jsp";
    }
    public String desc(HttpServletRequest request, HttpServletResponse response){
        String bid = request.getParameter("bid");
        Book book = mainService.getBookByid(bid);
        request.setAttribute("book",book);
        return "f:/jsps/book/desc.jsp";
    }

    public String search(HttpServletRequest request, HttpServletResponse response){
        String bname = request.getParameter("bname");
        String pageNow =request.getParameter("pageNow");
        if (pageNow==null || pageNow.equals("")){
            pageNow="1";
        }
        PageBean<Book> book = mainService.searchList(bname, Integer.parseInt(pageNow));
        book.setUrl("MainServlet?method=search&bname="+bname);
        request.setAttribute("bean",book);
        return "f:/jsps/book/list.jsp";
    }
    public String searchPro(HttpServletRequest request, HttpServletResponse response){
        String bname = request.getParameter("bname");
        String pageNow =request.getParameter("pageNow");
        String author = request.getParameter("author");
        String press = request.getParameter("press");
        if (pageNow==null || pageNow ==""){
            pageNow="1";
        }
        bname = bname == null ? "":bname ;
        author = author == null ? "":author ;
        press =press ==null ? "":press;
        PageBean<Book> book = mainService.searchProList(bname,author,press, Integer.valueOf(pageNow));
        book.setUrl("MainServlet?method=searchPro&bname="+bname+"&author="+author+"&press="+press);
        request.setAttribute("bean",book);
        return "f:/jsps/book/list.jsp";
    }
}
